<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	if(intval($_RQ['id'])){
		$this->session('game_id',intval($_RQ['id']));
	}
	$game_id=$this->session('game_id');
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($game_id){
		$params['game_id']=$game_id;
	}else{
		exi('请选择活动！','error','/merch.game/');
	}
	
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
		
	);
	$list=$this->get_list('hexiao_user',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),array('from_os','nickname','avatar','mobile','sex','pcd'));
			$item['from_os']=$item['member']['from_os']?get_ostitle()[$item['member']['from_os']]:'微信';
			$item['sex']=$item['member']['sex']==1?'男':'女';
			$pcd=$item['member']['pcd']?json_decode($item['member']['pcd'],true):array();
			$item['pcd']=$pcd['province'].' '.$pcd['city'];
			$return['params']['createtime']=$time_range;
			$item['createtime']=date('Y-m-d H:i',$item['createtime']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		//'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'核销员管理'),
			array('op'=>'edit','title'=>'添加核销员'),
		),
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：关于匿名用户的解释：正常认证服务号，没有关闭获取粉丝详细的情况下，是不会存在匿名用户的；可能小程序出现匿名用户的情况居多：原因是：小程序官方不允许首次访问弹窗获取粉丝详细。但是为了上下级的准确性，在访问小程序首页的时候就产生了下级关系！这时候就会出现匿名用户的情况。但是只要这个匿名用户访问了购物车，商品，会员中心等任何页面。就会弹窗获取粉丝信息。这时候匿名用户就会变成带昵称头像的会员了！</red>','del_one'=>true,'del_message'=>'确定删除分商商吗？会同时解除其下所有上下级关系哦！','delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'用户','type'=>'member'),
			array('name'=>'from_os','title'=>'来源','type'=>'string'),
			array('name'=>'pcd','title'=>'区域','type'=>'string'),
			array('name'=>'sex','title'=>'性别','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->pdo_get('hexiao_user',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['upass']='';
    $return['item']=$item;
	$htmls=array( 
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'核销员设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'核销员管理'),
			array('op'=>'edit','title'=>'添加核销员'),
		),
		'edit'=>array(
			array('name'=>'title','must'=>true,'title'=>'核销员名称','type'=>'string'),
			array('name'=>'game_id','title'=>'所属活动','type'=>'image'),
			array('name'=>'mid','title'=>'绑定管理员','type'=>'choose','action'=>'core/member.get_member/'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	if($_RQ['oneadd']){
		$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('from_os','nickname','avatar','mobile','sex','pcd'));
		$nickname=$member['nickname'];
		$hexiao_user=$this->pdo_get('hexiao_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'game_id'=>intval($_RQ['game_id'])),'id');
		if($hexiao_user['id']){
			exi('你已成为本活动的核销员！','error');
		}
		$data =array(
			'title'=>$nickname,
			'mid'=>intval($_SESSION['uid']),
			'game_id'=>intval($_RQ['game_id']),
		);
	}else{
		$post=get_postdata();
		if(!$post['title']){
			exi('核销员名称','error');
		}
		$data =array(
			'title'=>$post['title'],
			'mid'=>intval($post['mid']),
			'game_id'=>intval($post['game_id']),
		);
	}
	if($post['id']){
		$this->pdo_update('hexiao_user',$data,array('id'=>$post['id']));
		exi('保存成功','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		$this->pdo_insert('hexiao_user',$data);
		exi('添加成功','','list');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择商户！！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->pdo_delete('hexiao_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>